<?php

namespace addons\shopro\controller;
use think\Db;

class Feedback extends Base
{

    protected $noNeedLogin = ['type'];
    protected $noNeedRight = ['*'];


    public function type()
    {
        $this->success('反馈类型', array_values(\addons\shopro\model\Feedback::$typeAll));
    }


    public function add() {
        $params = $this->request->post();

        // 表单验证
        $this->shoproValidate($params, get_class(), 'add');

        $this->success('反馈成功', \addons\shopro\model\Feedback::add($params));
    }

    public function fastbuy()
    {
        $params = $this->request->post();

        $find = Db::name('shopro_fastbuy')->where([
            'user_id' => $this->auth->id,
            'createtime' => ['gt',time()-30]
        ])->find();
        if($find){
            $this->error('请30秒后再提交');
        }

        if(!$params['contact']){
            $this->error('请输入联系方式');
        }

        $_lists = [];
        $lists = $params['lists'];
        foreach ($lists as $k => $v) {
            if($v['name'] && $v['nums']){
                $_lists[] = $v;
            }
        }

        if(count($_lists)==0){
            $this->error('请至少补充一行的商品信息');
        }

        Db::name('shopro_fastbuy')->insert([
            'user_id' => $this->auth->id,
            'content' => json_encode($_lists),
            'contact' => $params['contact'],
            'createtime' => time()
        ]);

        $this->success('提交成功');
    }

}
